<template>
  <div class>
    <div class="card tr mb2">
      <el-button
        icon="el-icon-search"
        @click="formShow= !formShow"
        size="mini"
        type="primary"
        circle
      ></el-button>
      <el-button size="mini" type="primary" icon="el-icon-plus" @click="showDialog">添加用户</el-button>
    </div>
    <div class="card form" v-show="formShow">
      <el-form inline :model="searchForm" label-width="80px" ref="search">
        <el-form-item label="登陆账号" prop="username">
          <el-input v-model="searchForm.username" autocomplete="off" size="mini"></el-input>
        </el-form-item>
        <!-- <el-form-item label="组织机构" prop="organ">
          <el-cascader
            change-on-select
            :options="options"
            :props="cascaderProps"
            v-model="searchForm.organ"
          ></el-cascader>
        </el-form-item>-->

        <el-form-item>
          <el-button size="mini" @click="resetForm('search')">重置</el-button>
          <el-button size="mini" type="primary" @click="init">查询</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table stripe :data="tableData" border style="width: 100%">
      <el-table-column type="index" width="180px" fixed="left" align="center" label="序号"></el-table-column>
      <el-table-column width="180px" prop="realName" fixed="left" align="center" label="真实姓名"></el-table-column>
      <el-table-column width="180px" prop="username" fixed="left" align="center" label="登陆账号"></el-table-column>
      <el-table-column width="180px" prop="telephone" align="center" label="固定电话"></el-table-column>
      <el-table-column
        width="180px"
        prop="userStatus"
        align="center"
        label="启用状态"
        :formatter="filter2"
      ></el-table-column>
      <el-table-column width="180px" prop="createTime" align="center" label="创建日期"></el-table-column>
      <el-table-column width="250px" label="操作" fixed="right" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="showEditDialog(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="success" @click="showQxDialog(scope.$index, scope.row)">权限</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          <!-- <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">浏览</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <el-card style="text-align:right;">
      <el-pagination
        @size-change="sizeChange"
        @current-change="currentChange"
        :current-page="currentPage"
        :page-sizes="[ 10, 20,30, 40]"
        :page-size="pageSize"
        background
        layout="total, sizes, prev, pager, next"
        :total="total"
      ></el-pagination>
    </el-card>

    <el-dialog width="70%" title="角色权限" :visible.sync="dialogQsVisible">
      <el-checkbox-group v-model="checkbox.checkedRoles">
        <el-checkbox
          v-for="item in checkbox.Roles"
          :label="item.id"
          :key="item.id"
        >{{item.roleName}}</el-checkbox>
      </el-checkbox-group>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogQsVisible = false">取 消</el-button>
        <el-button type="primary" @click="changeRole">确 定</el-button>
      </div>
    </el-dialog>

    <add-form ref="addform" @addOver="init"></add-form>
    <edit-form ref="editform" :editForm="editForm" @editOver="init"></edit-form>
  </div>
</template>

<script>
import {
  addUserRole,
  getList,
  deleteItem,
  getRoleListByUserId
} from "./function";
import addForm from "./components/addForm";
import editForm from "./components/editForm";
import { getList as glbOptions } from "../jggl/organ";
import { getList as getRoleList } from "../jsgl/function";
export default {
  components: {
    addForm,
    editForm
  },
  data() {
    return {
      formShow: false,
      createUserName: JSON.parse(sessionStorage.getItem("user")).username,
      qxUserId: "",
      checkbox: {
        checkedRoles: [],
        Roles: []
      },
      dialogQsVisible: false,
      formLabelWidth: "80px",
      tableData: [],
      editForm: {},
      searchForm: {
        username: "",
        usertype: "",
        organ: ""
      },
      options: [],
      cascaderProps: {
        value: "id",
        label: "organName",
        children: "children"
      },

      total: 0,
      pageSize: 10,
      currentPage: 1
    };
  },
  computed: {},
  mounted() {
    this.init();
    this.getGlbOptions();
  },
  methods: {
    filter2(row, column, cellValue) {
      if (cellValue == 0) {
        return "不启用";
      }
      if (cellValue == 1) {
        return "启用";
      }
    },

    changeRole() {
      this.dialogQsVisible = false;

      let params = {
        userId: this.qxUserId,
        roleIds: this.checkbox.checkedRoles
      };
      addUserRole(params).then(res => {
        if (res.retHead.code == "0000000") {
          this.$message.success(res.retHead.message);
        } else {
          this.$message.error(res.retHead.message);
        }
      });
    },
    showQxDialog(i, row) {
      console.log(row);
      this.checkbox.checkedRoles = [];
      this.qxUserId = row.id;
      this.dialogQsVisible = true;
      getRoleList().then(res => {
        console.log(res);
        if (res.retHead.code == "0000000") {
          this.checkbox.Roles = res.retBody.list;
          this.getRoleListByUserId();
        }
      });
    },
    getRoleListByUserId() {
      let params = {
        userId: this.qxUserId
      };
      getRoleListByUserId(params).then(res => {
        console.log(res);
        if (res.retHead.code == "0000000") {
          let list = [];
          if (res.retBody.length > 0) {
            res.retBody.forEach(i => {
              list.push(i.id);
            });
          }
          this.checkbox.checkedRoles = list;
        }
      });
    },

    sizeChange(n) {
      this.pageSize = n;
      this.init();
    },
    currentChange(n) {
      this.currentPage = n;
      this.init();
    },
    getGlbOptions() {
      glbOptions().then(res => {
        console.log("ok");
        console.log(res.retBody);
        if (res.retHead.code == "0000000") {
          this.options = res.retBody;
        }
      });
    },
    init() {
      let params = {
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        username: this.searchForm.username,
        organCode: this.searchForm.organ[this.searchForm.organ.length - 1] || ""

        // usertype: this.searchForm.usertype
      };
      getList(params).then(res => {
        console.log(res);
        if (res.retHead.code == "0000000") {
          this.tableData = res.retBody.list;
          this.total = res.retBody.total;
          this.currentPage = res.retBody.pageNum;
        }
      });
    },
    showDialog() {
      this.$refs.addform.dialogFormVisible = true;
    },
    showEditDialog(index, row) {
      this.$refs.editform.dialogEditVisible = true;
      this.editForm = row;
      this.editForm.password = "";
      this.editForm.organCode = [row.organCode];
    },

    handleDelete(index, row) {
      this.$confirm("此操作将永久删除该项, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          deleteItem({ id: row.id }).then(res => {
            if (res.retHead.code == "0000000") {
              this.$message({
                message: "操作成功！",
                type: "success"
              });
              this.init();
            } else {
              this.$message({
                message: "操作失败！",
                type: "error"
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  created() {}
};
</script>
<style lang='scss' scoped>
.btns {
  text-align: right;
}
</style>
